
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.asterisk.manager.AuthenticationFailedException;
import net.sf.asterisk.manager.TimeoutException;

//BETA
import leica.wrapper.*;
import leica.xml.XMLTags;
import leica.xml.XMLTools;
import org.w3c.dom.Element;

//BETA
public class ConfController
    extends ApplicationInterface
    implements managerSIP.ManagerListener {


  private managerSIP.Manager myManagerSIP;

  public ConfController(String hostname, String username,
                                       String password) {
    myManagerSIP = new managerSIP.Manager(this, hostname, username, password);
  }

  public static void main(String[] args) {
    if (args.length < 3) {
      printUsage();
    }
    else {
      try {
        //Reading the arguments through the command line
        String hostname = args[0];
        String username = args[1];
        String password = args[2];
        @SuppressWarnings("unused")
            ConfController example =
            new ConfController(hostname, username, password);
        //example.test();
      }
      catch (Exception e) {
        e.printStackTrace();

      }
    }
  }

  static void printUsage() {
    System.out.println("Expected arguments: hostname username password");
  }

  //***********************************************************************
   // BETA ...
   // Implementing methods from managerSIP.ManagerListener



   /**
    * userJoinedConference
    *
    * @param sessionName String
    * @param conference int
    * @param userID String
    * @param user String
    */
   public void userJoinedConference(String sessionName, int conference,
                                    String userID, String user) {
     System.out.println("ConfController.userJoinedConference() " +
                        sessionName + "-- User " + userID +
                        " joins the conference " + conference);
     notify_userJoinedConference(sessionName, conference, userID, user);
   }

  /**
   * userLeaveConference
   *
   * @param sessionName String
   * @param conference int
   * @param userID String
   */
  public void userLeaveConference(String sessionName, int conference,
                                  String userID, String user) {
    System.out.println("ConfController.userLeaveConference()" +
                       sessionName + "-- User " + userID +
                       " leaves the conference " + conference);
    notify_userLeaveConference(sessionName, conference, userID, user);
  }

  /**
   * userJoinedSession
   *
   * @param session String
   * @param userID String
   */
  public void userJoinedSession(String session, String userID) {
    System.out.println("ConfController.userJoinedSession()" +
                       "--" + userID);
    notify_CONNECT(session, userID, "");
  }

  public void userLeaveSession(String session, String userID) {
    System.out.println("ConfController.userLeaveSession()" +
                       "--" + userID);
    notify_DISCONNECT(session, userID);
  }

  //***********************************************************************
   // BETA
   // Implement Abstract methods from leica.wrapper.ApplicationInterface

   public void do_createConference(String spSid, String[] usersList) {
     System.out.println("ConfController.do_createConference()" +
                        "-- " + spSid);
     //Parameter format conversion
     List <String> users = new ArrayList <String> ();
     
     for (int i = 0; i < usersList.length; i++) {
       users.add(usersList[i]);
       System.out.println("do_createConference: " + usersList[i] + " lista de usuarios\n");
     }
     try {
    	 System.out.println("do_createConference: spSid:" + spSid + "\n");
    	 myManagerSIP.createConference(spSid, users);
     }
     catch (IllegalArgumentException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     catch (IllegalStateException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     catch (AuthenticationFailedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     catch (TimeoutException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }

  public void do_createSpecificConference(String spSid, String[] usersList, int conf) {
    System.out.println("ConfController.do_createSpecificConference()" + "-- " +
        spSid);
    List < String > users = new ArrayList < String > ();
    for (int i = 0; i < usersList.length; i++) {
      users.add(usersList[i]);
    }
    try {
      try {
		myManagerSIP.createConference(spSid, users, conf);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (AuthenticationFailedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (TimeoutException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    }
    catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void do_changeConference(String spSid, java.lang.String user,
                                  int confSource, int confDest) {
    System.out.println("ConfController.do_changeConference()" +
                       "-- " + spSid);

    try {
      myManagerSIP.changeConference(user, spSid, confSource, confDest);
    }
    catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
    catch (IllegalStateException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
    catch (AuthenticationFailedException e) {
      e.printStackTrace();
    }
    catch (TimeoutException e) {
      e.printStackTrace();
    }
  }

  public void do_addUsersToConference(String spSid, String userDest, String[] usersSrc) {
	  System.out.println("ConfController.do_addUsersToConference()" + "-- " +
			  spSid);
	  List <String> users = new ArrayList <String> ();
	  System.out.println("ConfController.do_addUsersToConference: spSid:" + spSid + userDest +"\n");
	  
	  for (int i = 0; i < usersSrc.length; i++) {
		  System.out.println("do_addUsersToConference: users:" + usersSrc[i] +"\n");
		  users.add(usersSrc[i]);
	  }

	  try {
		  myManagerSIP.addUsersToConference (spSid, userDest, users);
	  }
	  catch (IllegalArgumentException e) {
		  e.printStackTrace();
	  }
	  catch (IllegalStateException e) {
		  e.printStackTrace();
	  }
	  catch (IOException e) {
		  e.printStackTrace();
	  }
	  catch (SQLException e) {
		  e.printStackTrace();
	  }
	  catch (AuthenticationFailedException e) {
		  e.printStackTrace();
	  }
	  catch (TimeoutException e) {
		  e.printStackTrace();
	  } catch (InterruptedException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
	  }
  }

  public void do_delUserConference(String spSid, java.lang.String user) {
    System.out.println("ConfController.do_delUserConference()" +
                       "-- " + spSid);
    try {
      myManagerSIP.delUserConference(spSid, user);
    }
    catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
    catch (IllegalStateException e) {
      e.printStackTrace();
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    catch (AuthenticationFailedException e) {
      e.printStackTrace();
    }
    catch (TimeoutException e) {
      e.printStackTrace();
    }
  }

  public void do_CONNECT(String spSid, java.lang.String user) {
    System.out.println("ConfController.do_CONNECT()" + "-- " +
                       spSid);
    try {
		myManagerSIP.addUserSession(spSid, user);
	} catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (AuthenticationFailedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (TimeoutException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }

  public void do_DISCONNECT(String spSid, String user) {
    System.out.println("ConfController.do_DISCONNECT()" + "-- " +
                       spSid);
    try {
      myManagerSIP.delUserSession(spSid, user);
    }
    catch (NumberFormatException e) {
      e.printStackTrace();
    }
    catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
    catch (IllegalStateException e) {
      e.printStackTrace();
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    catch (AuthenticationFailedException e) {
      e.printStackTrace();
    }
    catch (TimeoutException e) {
      e.printStackTrace();
    }
  }

  //********Implementing methods from "leica.wrapper.SessionMgtCommands"********

   public void createspS(Element params) {
     /*This element "params" contains the general spS structure + specifics defined in Asterisk.xsd
             <spS id="SessionName">
                <scheduling>
                   <starting>null</starting>
                   <duration>null</duration>
                </scheduling>
                <roles type="none" />
                <spSspecs>
                   <usersList>
                       <user> User1 </user>
                       ...
                       <user> UserN </user>
                   <usersList>
                </spSspecs>
             </spS>

      */
     //System.out.println("ConfController.createspS()" + "-- " + spSid);
     System.out.println("ConfController.createspS()" + "-- ");

     //Extracting information from this ...dom.Element
     String sessionName = XMLTools.getAttr(params, XMLTags.id_attr);
     Element[] userElements = XMLTools.getChildren(params, "user");
     //putting users into a ArrayList<String>
     List <String> users = new ArrayList <String> ();
     for (int i = 0; i < userElements.length; i++) {
       users.add(XMLTools.getText(userElements[i])); //Extracting each "UserI" from "<user> UserI </user>"
     }

     //Call the method of managerSIP responsible for creating new Sessions
     System.out.println(
         "ConfController.createspS(): Going to call "
         + " myManagerSIP.createSession(sessionName,users); ");
     myManagerSIP.createSession(sessionName, users);

   }

  public void terminatespS(String spSid) {
    System.out.println("ConfController.terminatespS()" + "-- " +
                       spSid +
                       "... NOT IMPLEMENTED!!!");
    //THE MANAGER DON'T HAVE ANY METHOD ALLOWING TO DELETE/TERMINATE A SESSION
  }

}